package com.dlc.felear.xiaoensale.activity;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.adapter.ActionItemStepAdapter;
import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.customView.ShowTispMsgDialog;
import com.dlc.felear.xiaoensale.dao.ActionStepItemDao;
import com.dlc.felear.xiaoensale.deviceUtil.TopbarHelper;
import com.dlc.felear.xiaoensale.entity.ActionStepItem;
import com.dlc.felear.xiaoensale.entity.HotInfo;
import com.dlc.felear.xiaoensale.helper.ActionHelper;
import com.dlc.felear.xiaoensale.helper.JsonHelper;
import com.itdlc.android.library.base.BaseActivity;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;
import org.simple.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 手动操作页面
 */
public class ManualDebugActivity extends BaseActivity  {
    private static final String TAG = "ManualDebugActivity";
    @BindView(R.id.m_gv1)
    GridView gridView1;
    @BindView(R.id.m_gv2)
    GridView gridView2;
    @BindView(R.id.m_gv3)
    GridView gridView3;
    private ActionItemStepAdapter actionItemStepAdapter;
    private ActionItemStepAdapter actionItemStepAdapter1;
    private ActionItemStepAdapter actionItemStepAdapter2;
    private ActionHelper actionHelper;
    private TopbarHelper topbarHelper;
    private ActionStepItemDao actionStepItemDao;
    @BindView(R.id.tv_power)
    TextView tvPower;
    @BindView(R.id.tv_temp)
    TextView tvTemp;
    @BindView(R.id.tv_error)
    TextView tvError;
    @BindView(R.id.sd_tv_start_record)
    TextView tvRecord;
    @BindView(R.id.sd_tv_query_record)
    TextView tvQuery;

    /**
     * 信息显示框
     */
    private ShowTispMsgDialog dialog;



    @Override
    protected void initialView() {
        dialog=new ShowTispMsgDialog(this);
        dialog.setCanceledOnTouchOutside(true);
        actionHelper=new ActionHelper(this);
        topbarHelper=new TopbarHelper(this);
        topbarHelper.setTitle("手动操作");
        topbarHelper.setNavActivity(CcLauncherActivity.class);
        actionStepItemDao=new ActionStepItemDao(this);
        actionHelper.setTvPower((TextView) findViewById(R.id.tv_power),(TextView) findViewById(R.id.tv_temp),(TextView) findViewById(R.id.tv_error));
        initGridView();
    }

    @Override
    protected int getLayoutRes() {
        EventBus.getDefault().register(this);
        return R.layout.activity_manual_debug;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
//        EventBus.getDefault().post(false,Constant.AUTO_FLAG);
    }


    private void initGridView(){
        actionItemStepAdapter=new ActionItemStepAdapter(this);
        gridView1.setAdapter(actionItemStepAdapter);
        actionItemStepAdapter.setItems(getGtdz());
        gridView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                actionHelper.parserAction(1,actionItemStepAdapter.getItem(position));
            }
        });

        actionItemStepAdapter1=new ActionItemStepAdapter(this);
        gridView2.setAdapter(actionItemStepAdapter1);
        actionItemStepAdapter1.setItems(actionStepItemDao.queryForAllByAction(Constant.ACTION_STEP_JGT));
        gridView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                actionHelper.parserAction(2,actionItemStepAdapter1.getItem(position));
            }
        });

        actionItemStepAdapter2=new ActionItemStepAdapter(this);
        gridView3.setAdapter(actionItemStepAdapter2);
        actionItemStepAdapter2.setItems(actionStepItemDao.queryForAllByAction(Constant.ACTION_STEP_JYT));
        gridView3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                actionHelper.parserAction(3,actionItemStepAdapter2.getItem(position));
            }
        });

    }


    public List<ActionStepItem> getGtdz() {
        List<ActionStepItem> data=new ArrayList<>();
        for(ActionStepItem temp:actionStepItemDao.queryForAllByAction(Constant.ACTION_STEP_GDZ)){
            if(TextUtils.equals(temp.getName(),Constant.ACTION_NAME_DS)
                    ||TextUtils.equals(temp.getName(),Constant.ACTION_NAME_CCSC)
                    ||TextUtils.equals(temp.getName(),Constant.ACTION_NAME_JC)
                    ||TextUtils.equals(temp.getName(),Constant.ACTION_NAME_XG)
                    ||TextUtils.equals(temp.getName(),Constant.ACTION_NAME_JGG)){
                continue;
            }
            data.add(temp);
        }

        return data;
    }


    @Subscriber(tag = Constant.ACTION_HOT_INFO_RESULT)
    public void recieveHotInfo(HotInfo hotInfo) {
        Log.e(TAG, JsonHelper.toJson(hotInfo));
        if(hotInfo!=null){
            tvError.setText(hotInfo.getMsg());
            tvPower.setText(hotInfo.getPower()+"档");
            tvTemp.setText(hotInfo.getTemp()+"℃");
        }
    }

    @Subscriber(tag =Constant.DEVICE_STATUS_MSG_SHOW)
    public void warnmingMsg(String msg) {
        Log.e(TAG,"warnmingMsg="+msg);
        showLongToast(msg);
    }
    @Subscriber( tag = Constant.DEVICE_ERROR_MSG_SHOW)
    public void warnErrorMsg(String msg) {
        if(TextUtils.isEmpty(msg)){
            dialog.closeDialog();
            return;
        }
        dialog.setMsg(msg);
        dialog.setDialog("设备故障");
    }

}
